unit KRK.Components.StandardControls.Panel;

interface

uses ExtCtrls, Graphics, Classes, GraphUtil;

type
  TEdge = (eUnknown,eTopLeft,eTopRight,eBottomLeft,eBottomRight);

  TKRKPanel = class(TPanel)
  private
    FGradientFill: Boolean;
    FGradientColorA: TColor;
    FGradientColorB: TColor;
    FGradientDirection: TGradientDirection;
    procedure SetGradientFill(const Value: Boolean);
    function GetParentBackground: Boolean;
    procedure SetGradientColorA(const Value: TColor);
    procedure SetGradientColorB(const Value: TColor);
    procedure SetGradientDirection(const Value: TGradientDirection);
  protected
    procedure SetParentBackground(Value: Boolean); override;
    procedure Paint; override;
  public
    constructor Create(aOwner: TComponent); override;
  published
    property ParentBackground: Boolean read GetParentBackground write SetParentBackground default True;
    property GradientFill: Boolean read FGradientFill write SetGradientFill default False;
    property GradientColorA: TColor read FGradientColorA write SetGradientColorA default clGradientInactiveCaption;
    property GradientColorB: TColor read FGradientColorB write SetGradientColorB default clActiveCaption;
    property GradientDirection: TGradientDirection read FGradientDirection write SetGradientDirection default gdVertical;
  end;

implementation

uses
  Types, Windows, Controls, Math;

{ TKRKPanel }

constructor TKRKPanel.Create(aOwner: TComponent);
begin
  inherited;
  FGradientFill := False;
  FGradientColorA := clGradientInactiveCaption;
  FGradientColorB := clActiveCaption;
  FGradientDirection := gdVertical;
end;

function TKRKPanel.GetParentBackground: Boolean;
begin
  Result := inherited ParentBackground;
end;

procedure TKRKPanel.Paint;
{ ---------------------------------------------------------------------------- }
function TotalBevel: Word;
begin
  Result := 0;

  if BevelInner <> bvNone then
    Inc(Result,BevelWidth);

  if BevelOuter <> bvNone then
    Inc(Result,BevelWidth);
end;
{ ---------------------------------------------------------------------------- }
var
  PaintRect: TRect;
  FullBevelWidth: Word;
begin
  inherited;

  if FGradientFill then
  begin
    PaintRect := GetClientRect;
    FullBevelWidth := TotalBevel;
    InflateRect(PaintRect,-FullBevelWidth,-FullBevelWidth);

    if (BevelInner <> bvNone) or (BevelOuter <> bvNone) then
    begin

      if not (beTop in BevelEdges) then
        PaintRect.Top := PaintRect.Top - FullBevelWidth;

      if not (beLeft in BevelEdges) then
        PaintRect.Left := PaintRect.Left - FullBevelWidth;

      if not (beBottom in BevelEdges) then
        PaintRect.Bottom := PaintRect.Bottom + FullBevelWidth;

      if not (beRight in BevelEdges) then
        PaintRect.Right := PaintRect.Right + FullBevelWidth;
    end;

    GradientFillCanvas(Self.Canvas,FGradientColorA,FGradientColorB,PaintRect,FGradientDirection)
  end
end;

procedure TKRKPanel.SetGradientColorA(const Value: TColor);
begin
  FGradientColorA := Value;
  Invalidate;
end;

procedure TKRKPanel.SetGradientColorB(const Value: TColor);
begin
  FGradientColorB := Value;
  Invalidate;
end;

procedure TKRKPanel.SetGradientDirection(const Value: TGradientDirection);
begin
  FGradientDirection := Value;
  Invalidate;
end;

procedure TKRKPanel.SetGradientFill(const Value: Boolean);
begin
  FGradientFill := Value;

  if FGradientFill then
    ParentBackground := False;

  Invalidate;
end;

procedure TKRKPanel.SetParentBackground(Value: Boolean);
begin
  inherited;

  if ParentBackground then
    FGradientFill := False;
end;

end.
